package mortal.game.control
{
    import Framework.MQ.*;
    import Message.Command.*;
    import Message.Game.*;
    import flash.events.*;
    import flash.utils.*;
    import modules.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.view.system.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class SystemSettingController extends Controller
    {
        private var _sysSetInfoObj:Object;
        private var _isSet:Boolean = false;
        private var _isSetDefault:Boolean = false;
        private var _sysSetterInfo:SystemSetterInfo;
        private var _timer:Timer;
        private var _sysSetInfoStr:String;

        public function SystemSettingController()
        {
            this._sysSetterInfo = new SystemSetterInfo();
            this._timer = new Timer(500, 2);
            
        }

        override protected function initView() : IView
        {
            var _loc_1:* = new SystemSettingModule();
            return _loc_1;
        }

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.SaveSystemSetter, this.onSaveSystemSetHandler);
            Dispatcher.addEventListener(EventName.SetSysSetterPanel, this.onSetSystemInfoSucHandler);
            Dispatcher.addEventListener(EventName.SaveForSliderChange, this.onSaveSliderSetHandler);
            Dispatcher.addEventListener(EventName.BackGroundMusicOpenOrClose, this.onBackGroundMusicOpenOrCloseHandler);
            Dispatcher.addEventListener(EventName.Sys_SaveInfoNotShowInWindow, this.onSaveInfoNotShowInWindowHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateSysSetting, this.onReceiveSysSetterHandler);
            
        }

        private function onSaveSystemSetHandler(event:DataEvent) : void
        {
            this._isSet = true;
            if (event.data != null)
            {
                this._sysSetInfoStr = event.data as String;
            }
            else
            {
                this._sysSetInfoStr = SystemSetter.localSetter.getString();
            }
            GameProxy.sysSetterProxy.saveSystemSetterInfo(this._sysSetInfoStr);
            this.setSystemSetterInfo(this._sysSetInfoStr);
            
        }

        private function onSaveInfoNotShowInWindowHandler(event:DataEvent) : void
        {
            this._sysSetInfoStr = SystemSetter.localSetter.getString();
            GameProxy.sysSetterProxy.saveSystemSetterInfo(this._sysSetInfoStr);
            this.setSystemSetterInfo(this._sysSetInfoStr);
            
        }

        private function onSaveSliderSetHandler(event:DataEvent) : void
        {
            if (!this._timer.running)
            {
                this._timer.start();
                this._timer.addEventListener(TimerEvent.TIMER_COMPLETE, this.onTimerCompleteHandler);
            }
            
        }

        private function onTimerCompleteHandler(event:TimerEvent) : void
        {
            this._sysSetInfoStr = SystemSetter.localSetter.getString();
            GameProxy.sysSetterProxy.saveSystemSetterInfo(this._sysSetInfoStr);
            this.setSystemSetterInfo(this._sysSetInfoStr);
            this._timer.stop();
            this._timer.reset();
            this._timer.removeEventListener(TimerEvent.TIMER_COMPLETE, this.onTimerCompleteHandler);
            
        }

        private function onSetSystemInfoSucHandler(event:DataEvent) : void
        {
            if (!view.isHide)
            {
                (view as SystemSettingModule).resetView(this._sysSetInfoObj);
            }
            
        }

        private function onReceiveSysSetterHandler(param1:MessageBlock) : void
        {
            var _loc_4:String = null;
            var _loc_2:* = param1.messageBase as SSysSetting;
            var _loc_3:* = _loc_2.jsStr;
            if (_loc_3 != "")
            {
                this._sysSetInfoObj = SystemSetter.currentSetter.deserializeInfo(_loc_3);
                SystemSetter.localSetter.copy(SystemSetter.currentSetter);
                (view as SystemSettingModule).resetView(this._sysSetInfoObj);
                cache.autoResumeSet.roleLifeSet = this._sysSetInfoObj["roleLife"] as Number;
                cache.autoResumeSet.roleManaSet = this._sysSetInfoObj["roleMana"] as Number;
                cache.autoResumeSet.petLifeSet = this._sysSetInfoObj["petLife"] as Number;
                cache.autoResumeSet.petManaSet = this._sysSetInfoObj["petMana"] as Number;
                cache.autoResumeSet.isSaveSysset = true;
                Dispatcher.dispatchEvent(new DataEvent(EventName.SysSettingSuccess, this._sysSetInfoObj));
            }
            else if (!this._isSetDefault)
            {
                this._isSetDefault = true;
                _loc_4 = SystemSetter.currentSetter.getString();
                GameProxy.sysSetterProxy.saveSystemSetterInfo(_loc_4);
                this.setSystemSetterInfo(_loc_4);
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.NoSoundBtnShowOrHide, SystemSetter.currentSetter.haveNoSound));
            Dispatcher.dispatchEvent(new DataEvent(EventName.SysSettingRes));
            
        }

        private function onBackGroundMusicOpenOrCloseHandler(event:DataEvent) : void
        {
            (view as SystemSettingModule).haveNoSound = !(view as SystemSettingModule).haveNoSound;
            
        }

        private function setSystemSetterInfo(param1:String) : void
        {
            this._sysSetInfoObj = SystemSetter.currentSetter.deserializeInfo(param1);
            cache.autoResumeSet.roleLifeSet = this._sysSetInfoObj["roleLife"] as Number;
            cache.autoResumeSet.roleManaSet = this._sysSetInfoObj["roleMana"] as Number;
            cache.autoResumeSet.petLifeSet = this._sysSetInfoObj["petLife"] as Number;
            cache.autoResumeSet.petManaSet = this._sysSetInfoObj["petMana"] as Number;
            cache.autoResumeSet.isSaveSysset = true;
            if (this._isSet)
            {
                MsgManager.addBroadCast("系统设置成功");
                this._isSet = false;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.SysSettingSuccess, this._sysSetInfoObj));
            
        }

    }
}
